package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.org.*;
import cn.juque.systemservice.api.provider.IOrgInfoProvider;
import cn.juque.systemservice.convert.OrgConvertor;
import cn.juque.systemservice.domain.OrgInfoDO;
import cn.juque.systemservice.entity.OrgInfo;
import cn.juque.systemservice.service.IOrgInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>机构信息</li>
 * </ul>
 * @date 2022-11-03 23:57:34
 **/
@Slf4j
@Service("orgInfoProvider")
@HandlerAppException
public class OrgInfoProviderImpl implements IOrgInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IOrgInfoService orgInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<OrgInfoListDTO>> list(OrgInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        OrgInfo orgInfo = new OrgInfo();
        BeanUtil.copyProperties(searchDTO, orgInfo);
        List<OrgInfoDO> infoList = this.orgInfoService.listInfoWithoutPage(orgInfo);
        List<OrgInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, OrgInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return OrgInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<OrgInfoDetailDTO> detail(String id) {
        OrgInfo orgInfo = this.orgInfoService.detail(id);
        OrgInfoDetailDTO detailDTO = new OrgInfoDetailDTO();
        BeanUtil.copyProperties(orgInfo, detailDTO);
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 根据主键获取机构信息
     *
     * @param req 主键列表
     * @return BaseResponseDTO
     */
    @Override
    public BaseResponseDTO<List<OrgInfoDetailDTO>> getByIds(OrgSearchByIdDTO req) {
        this.validatorUtil.fastValidate(req);
        List<OrgInfoDO> list = this.orgInfoService.getByIds(req.getOrgIdList());
        List<OrgInfoDetailDTO> dtoList = OrgConvertor.toOrgInfoDetailDtoList(list, req.getContainChild());
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(OrgInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        OrgInfo orgInfo = new OrgInfo();
        BeanUtil.copyProperties(addDTO, orgInfo);
        this.orgInfoService.saveInfo(orgInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(OrgInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        OrgInfo orgInfo = new OrgInfo();
        BeanUtil.copyProperties(updateDTO, orgInfo);
        this.orgInfoService.updateInfo(orgInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(OrgInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.orgInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
